package com.dhcc.bpm.alarm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.alarm.entity.AlarmStrategy;
import com.dhcc.bpm.alarm.service.IAlarmStrategyInfoService;
import com.dhcc.bpm.alarm.vo.AlarmStrategyVO;
import com.dhcc.bpm.modules.system.service.ISerialNumberService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 控制器
 *
 * @author xiafayu
 * @since 2022-02-16
 */
@RestController
@AllArgsConstructor
@NoArgsConstructor
@RequestMapping("alarm/alarmStrategy")
@Api(value = "预警策略表", tags = "接口")
public class AlarmStrategyInfoController extends BladeController {
    @Autowired
    private IAlarmStrategyInfoService alarmStrategyService;
    @Autowired
    private ISerialNumberService serialNumberService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入alarmStrategy")
    public R<AlarmStrategy> detail(AlarmStrategy alarmStrategy) {
        AlarmStrategy detail = alarmStrategyService.getOne(Condition.getQueryWrapper(alarmStrategy));
        return R.data(detail);
    }

    /**
     * 分页
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入alarmStrategy")
    public R<IPage<AlarmStrategy>> list(AlarmStrategy alarmStrategy, Query query) {
        IPage<AlarmStrategy> pages = alarmStrategyService.page(Condition.getPage(query), Condition.getQueryWrapper(alarmStrategy));
        return R.data(pages);
    }

    /**
     * 自定义分页
     */
    @GetMapping("/qryList")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "传入alarmStrategy")
    public R<IPage<AlarmStrategyVO>> page(AlarmStrategyVO alarmStrategy, Query query) {
        IPage<AlarmStrategyVO> pages = alarmStrategyService.selectAlarmStrategyPage(Condition.getPage(query), alarmStrategy);
        return R.data(pages);
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入alarmStrategy")
    public R save(@Valid @RequestBody AlarmStrategy alarmStrategy) {
        System.out.println("131\n131\n131\n131\n131\n131\n131\n131\n");
        return R.status(alarmStrategyService.save(alarmStrategy));
    }

    /**
     * 修改
     */
    @PostMapping("/edit")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入alarmStrategy")
    public R update(@Valid @RequestBody AlarmStrategy alarmStrategy) {
        return R.status(alarmStrategyService.updateById(alarmStrategy));
    }

    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入alarmStrategy")
    public R submit(@Valid @RequestBody AlarmStrategy alarmStrategy) {
        if (null == alarmStrategy.getId()) {
            String noticeCode = serialNumberService.generateSerialNumberByModelCode("strategy_code");
            alarmStrategy.setStrategyCode(noticeCode);
        }
        
        if("02".equals(alarmStrategy.getExecMethod())) {
        	if(!isSelect(alarmStrategy.getRuleScript())) {
        		R.fail("sql语句只能是select！");
        	}
        }
        
        return R.status(alarmStrategyService.saveOrUpdate(alarmStrategy));
    }
    
    //用正则表达式验证sql是否select语句
    private static boolean isSelect(String sql) {
        return sql.matches("^(?i)(\\s*)(select)(\\s+)(((?!(insert|delete|update|truncate|alter)).)+)$");
    }


    /**
     * 删除
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(alarmStrategyService.removeByIds(Func.toLongList(ids)));
    }


}
